var express = require('express');
var router = express.Router();
var linkService = require('../../src/service/linkService');
var resultMessage = require('../../src/models/resultMessage');

var result = new resultMessage();
var linkImpl = new linkService();

router.post('/loadTypes', function (req, res, next) {
    var uid = req.session.user.id;
    var parentId = req.body.parentId;

    linkImpl.loadTypes(uid, parentId, function (rows) {
        res.send(result.ok(rows).toString());
    });
});
router.post('/loadLinks', function (req, res, next) {
    var uid = req.session.user.id;
    var typeId = req.body.typeId;
    var top = req.body.top;

    linkImpl.loadLinks(uid, typeId, top, function (rows) {
        res.send(result.ok(rows).toString());
    });
});
router.post('/visit', function (req, res, next) {
    var uid = req.session.user.id;
    var linkId = req.body.linkId;

    linkImpl.visit(uid, linkId, function () {
        res.send(result.ok().toString());
    });
});
router.post('/frequently', function (req, res, next) {
    var uid = req.session.user.id;
    var count = req.body.count;

    linkImpl.listFrequently(uid, count, function (rows) {
        res.send(result.ok(rows).toString());
    });
});
router.post('/collapse', function (req, res, next) {
    var uid = req.session.user.id;
    var typeId = req.body.typeId;
    var state = req.body.state;

    linkImpl.updateCollapse(uid, typeId, state, function () {
        res.send(result.ok().toString());
    });
});
router.post('/type/:id', function (req, res, next) {
    var uid = req.session.user.id;
    var typeId = req.params.id;

    linkImpl.getType(uid, typeId, function (row) {
        res.send(result.ok(row).toString());
    });
});
router.post('/delete', function (req, res, next) {
    var uid = req.session.user.id;
    var linkId = req.body.linkId;

    linkImpl.deleteLink(uid, linkId, function () {
        res.send(result.ok().toString());
    });
});
router.post('/moveType', function (req, res, next) {
    var uid = req.session.user.id;
    var typeId = req.body.typeId;
    var parentId = req.body.parentId;

    linkImpl.moveType(uid, typeId, parentId, function () {
        res.send(result.ok().toString());
    });
});
router.post('/moveLink', function (req, res, next) {
    var uid = req.session.user.id;
    var typeId = req.body.typeId;
    var linkId = req.body.linkId;

    linkImpl.moveLink(uid, linkId, typeId, function () {
        res.send(result.ok().toString());
    });
});
router.post('/addType', function (req, res, next) {
    var uid = req.session.user.id;
    var parentId = req.body.parentId;
    var name = req.body.name;
    var description = req.body.description;

    linkImpl.addType(uid, parentId, name, description, function () {
        res.send(result.ok().toString());
    });
});
router.post('/addLink', function (req, res, next) {
    var uid = req.session.user.id;
    var typeId = req.body.typeId;
    var name = req.body.name;
    var url = req.body.url;
    var description = req.body.description;

    linkImpl.addLink(uid, typeId, name, url, description, function () {
        res.send(result.ok().toString());
    });
});
router.post('/search', function (req, res, next) {
    var uid = req.session.user.id;
    var key = req.body.key;

    linkImpl.searchLink(uid, key, function (rows) {
        res.send(result.ok(rows).toString());
    });
});

module.exports = router;
